package org.anyin.gitee.cloud.center.upms.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.cloud.center.upms.convert.SysUserConvert;
import org.anyin.gitee.cloud.center.upms.enums.UpmsExCodeEnum;
import org.anyin.gitee.cloud.center.upms.model.SysUser;
import org.anyin.gitee.cloud.center.upms.model.enmus.SysUserStatusEnum;
import org.anyin.gitee.cloud.center.upms.model.ext.SysUserExt;
import org.anyin.gitee.cloud.center.upms.repository.SysUserRepository;
import org.anyin.gitee.cloud.center.upms.service.SysUserService;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserPageDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserSaveDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysUserConvert sysUserConvert;

    @Override
    public SysUserRepository getRepository() {
        return sysUserRepository;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class, timeout = 60)
    public void create(SysUserSaveDTO info) {
        SysUser model = sysUserConvert.getSysUser(info);
        String salt = RandomUtil.randomString(5);
        String password = SecureUtil.md5(info.getPassword() + salt);
        model.setPassword(password);
        model.setStatus(SysUserStatusEnum.ENABLE.getCode());
        model.setSalt(salt);
        sysUserRepository.save(model);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class, timeout = 60)
    public void modify(Long id, SysUserSaveDTO info) {
        SysUser model = sysUserConvert.getSysUser(info);
        model.setId(id);
        String salt = RandomUtil.randomString(5);
        String password = SecureUtil.md5(info.getPassword() + salt);
        model.setPassword(password);
        model.setSalt(salt);
        sysUserRepository.updateById(model);
    }

    @Override
    public SysUserInfoDTO info(Long id) {
        SysUser model = sysUserRepository.getById(id);
        if(model == null){
            throw UpmsExCodeEnum.USER_NOT_FOUND.getException();
        }
        SysUserInfoDTO user = sysUserConvert.getSysUserInfoDTO(model);
        return user;
    }

    @Override
    public void delete(Long id) {
        // check has role
        sysUserRepository.removeById(id);
    }

    @Override
    public Page<SysUserExt> page(SysUserPageDTO query) {
         String filed = query.getType().getCode();
        SysUser model = new SysUser();
        ReflectUtil.invoke(model, "set" + CharSequenceUtil.upperFirst(filed), query.getKeyword());
        model.setUsername(query.getKeyword());
        Page<SysUserExt> page = sysUserRepository.page(query.getPageNo(), query.getPageSize(), model);
        return page;
    }
}
